//
// SoapTrace.Net - A soap message monitoring utility.
//
// Copyright (C) 2004 Matthew Ward
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Matthew Ward (mrward@users.sourceforge.net)

using NUnit.Framework;
using SoapTrace.Core.Formatters;
using SoapTrace.Tests.Utils;
using SoapTrace.Tests.Config;
using System;
using System.Configuration;
using System.IO;

namespace SoapTrace.Tests
{
	/// <summary>
	/// Tests the parsing of a Rpc wsdl string.
	/// </summary>
	[ TestFixture ]
	public class TextEJBWebServiceTestFixture
	{
		/// <summary>
		/// The web service read from the wsdl file.
		/// </summary>
		WebService WebService;
		
		/// <summary>
		/// The web service that we expect to be
		/// read from the wsdl.
		/// </summary>
		WebService ExpectedWebService;
		
		/// <summary>
		/// Initialises each test.
		/// </summary>
		[ SetUp ]
		public void Init( )
		{
			WebServicesDescriptionLanguageFile Wsdl = new WebServicesDescriptionLanguageFile( );
			Wsdl.Load( new StringReader( GetWsdl( ) ) );
			          
			WebService = Wsdl.WebService;
						
			InitExpectedWebService( );
		}
		
		/// <summary>
		/// Tests that the web service name is extracted correctly.
		/// </summary>
		[ Test ]
		public void WebServiceName( )
		{
			Assert.AreEqual( ExpectedWebService.Name, WebService.Name, "Web service name is incorrect." );
		}
		
		/// <summary>
		/// Check the uri extracted from the wsdl file is correct.
		/// </summary>
		[ Test ]
		public void WebServiceUri( )
		{
			Assert.AreEqual( ExpectedWebService.Uri, WebService.Uri,
				"Web service uri is incorrect." );
		}
		
		/// <summary>
		/// Tests that the web service operations have been extracted.
		/// </summary>
		[ Test ]
		public void Operations( )
		{
			Assert.AreEqual( ExpectedWebService.Operations.Count, 
				WebService.Operations.Count, 
				"Web service count is incorrect." );
			
			for( int i = 0; i < ExpectedWebService.Operations.Count; ++i )
			{
				WebServiceOperation ExpectedOperation = ExpectedWebService.Operations[ i ];
				WebServiceOperation ActualOperation = WebService.Operations[ i ];
				
				Assert.AreEqual( ExpectedOperation.Name, 
				                ActualOperation.Name, 
				                "Web service operation name mismatch" );
				
				Assert.AreEqual( ExpectedOperation.Parameters.Count, 
				                ActualOperation.Parameters.Count,
				                String.Concat( "Web service params count mismatch.  Name=",
				                              ExpectedOperation.Name ) );
				
				WebServiceTestUtility.CompareParameters( ExpectedOperation, ActualOperation, "Param mismatch.  Name=" + ExpectedOperation.Name );
			
				WebServiceTestUtility.CompareReturnValue( ExpectedOperation, ActualOperation, "Return value mismatch.  Name=" + ExpectedOperation.Name );
			}
		}
		
		/// <summary>
		/// Sets up the <see cref="ExpectedWebService"/>.
		/// </summary>
		private void InitExpectedWebService( )
		{
			ExpectedWebService = new WebService( );
			
			ExpectedWebService.Name = "TextEJB";
			ExpectedWebService.Uri = "/TextEJB.asmx";
			
			// Set up operation
			ExpectedWebService.Operations.Add( GetCompressOperation( ) );
			ExpectedWebService.Operations.Add( GetUncompressOperation( ) );
			ExpectedWebService.Operations.Add( GetCompressListOperation( ) );
			ExpectedWebService.Operations.Add( GetUncompressListOperation( ) );
		}
		
		/// <summary>
		/// Creates a <see cref="WebServiceOperation"/> for the
		/// TextEJB.Compress web service.
		/// </summary>
		/// <returns>A <see cref="WebServiceOperation"/>.</returns>
		private WebServiceOperation GetCompressOperation( )
		{
			WebServiceOperation Operation = new WebServiceOperation( );
			Operation.Name = "Compress";
			
			WebServiceParameter Param = new WebServiceParameter( );
			Param.Name = "string";
			Param.ParameterType = "string";
			
			Operation.Parameters.Add( Param );
						
			WebServiceReturnValue ReturnValue = new WebServiceReturnValue( );
			ReturnValue.Name = "result";
			ReturnValue.ParameterType = "string";

			Operation.ReturnValue = ReturnValue;
			
			return Operation;
		}
		
		/// <summary>
		/// Creates a <see cref="WebServiceOperation"/> for the
		/// TextEJB.Uncompress web service.
		/// </summary>
		/// <returns>A <see cref="WebServiceOperation"/>.</returns>
		private WebServiceOperation GetUncompressOperation( )
		{
			WebServiceOperation Operation = new WebServiceOperation( );
			Operation.Name = "Uncompress";
			
			WebServiceParameter Param = new WebServiceParameter( );
			Param.Name = "string";
			Param.ParameterType = "string";
			
			Operation.Parameters.Add( Param );
					
			WebServiceReturnValue ReturnValue = new WebServiceReturnValue( );
			ReturnValue.Name = "result";
			ReturnValue.ParameterType = "string";

			Operation.ReturnValue = ReturnValue;

			return Operation;
		}		
		
		/// <summary>
		/// Creates a <see cref="WebServiceOperation"/> for the
		/// TextEJB.CompressList web service.
		/// </summary>
		/// <returns>A <see cref="WebServiceOperation"/>.</returns>
		private WebServiceOperation GetCompressListOperation( )
		{
			WebServiceOperation Operation = new WebServiceOperation( );
			Operation.Name = "CompressList";
			
			WebServiceParameter Param = new WebServiceParameter( );
			Param.Name = "string";
			Param.ParameterType = "string.array";
			
			Operation.Parameters.Add( Param );
						
			WebServiceReturnValue ReturnValue = new WebServiceReturnValue( );
			ReturnValue.Name = "result";
			ReturnValue.ParameterType = "string.array";

			Operation.ReturnValue = ReturnValue;
			
			return Operation;
		}	
		
		/// <summary>
		/// Creates a <see cref="WebServiceOperation"/> for the
		/// TextEJB.UncompressList web service.
		/// </summary>
		/// <returns>A <see cref="WebServiceOperation"/>.</returns>
		private WebServiceOperation GetUncompressListOperation( )
		{
			WebServiceOperation Operation = new WebServiceOperation( );
			Operation.Name = "UncompressList";
			
			WebServiceParameter Param = new WebServiceParameter( );
			Param.Name = "string";
			Param.ParameterType = "string.array";
			
			Operation.Parameters.Add( Param );
						
			WebServiceReturnValue ReturnValue = new WebServiceReturnValue( );
			ReturnValue.Name = "result";
			ReturnValue.ParameterType = "string.array";

			Operation.ReturnValue = ReturnValue;
			
			return Operation;
		}		
		
		/// <summary>
		/// Gets the document literal wsdl string.
		/// </summary>
		/// <returns>A wsdl string.</returns>
		private string GetWsdl( )
		{
			return "<definitions xmlns:http=\"http://schemas.xmlsoap.org/wsdl/http/\" xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\" xmlns:s=\"http://www.w3.org/2001/XMLSchema\" xmlns:s0=\"urn:Text/encodedTypes\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:tns=\"urn:Text\" xmlns:tm=\"http://microsoft.com/wsdl/mime/textMatching/\" xmlns:mime=\"http://schemas.xmlsoap.org/wsdl/mime/\" targetNamespace=\"urn:Text\" xmlns=\"http://schemas.xmlsoap.org/wsdl/\">\r\n" +
				"  <types>\r\n" +
				"    <s:schema targetNamespace=\"urn:Text/encodedTypes\">\r\n" +
				"      <s:import namespace=\"http://schemas.xmlsoap.org/soap/encoding/\" />\r\n" +
				"      <s:import namespace=\"http://schemas.xmlsoap.org/wsdl/\" />\r\n" +
				"      <s:complexType name=\"ArrayOfString\">\r\n" +
				"        <s:complexContent mixed=\"false\">\r\n" +
				"          <s:restriction base=\"soapenc:Array\">\r\n" +
				"            <s:attribute d7p1:arrayType=\"s:string[]\" ref=\"soapenc:arrayType\" xmlns:d7p1=\"http://schemas.xmlsoap.org/wsdl/\" />\r\n" +
				"\r\n" +
				"          </s:restriction>\r\n" +
				"        </s:complexContent>\r\n" +
				"      </s:complexType>\r\n" +
				"    </s:schema>\r\n" +
				"  </types>\r\n" +
				"  <message name=\"CompressSoapIn\">\r\n" +
				"    <part name=\"string\" type=\"s:string\" />\r\n" +
				"  </message>\r\n" +
				"  <message name=\"CompressSoapOut\">\r\n" +
				"\r\n" +
				"    <part name=\"result\" type=\"s:string\" />\r\n" +
				"  </message>\r\n" +
				"  <message name=\"UncompressSoapIn\">\r\n" +
				"    <part name=\"string\" type=\"s:string\" />\r\n" +
				"  </message>\r\n" +
				"  <message name=\"UncompressSoapOut\">\r\n" +
				"    <part name=\"result\" type=\"s:string\" />\r\n" +
				"  </message>\r\n" +
				"  <message name=\"CompressListSoapIn\">\r\n" +
				"\r\n" +
				"    <part name=\"string\" type=\"s0:ArrayOfString\" />\r\n" +
				"  </message>\r\n" +
				"  <message name=\"CompressListSoapOut\">\r\n" +
				"    <part name=\"result\" type=\"s0:ArrayOfString\" />\r\n" +
				"  </message>\r\n" +
				"  <message name=\"UncompressListSoapIn\">\r\n" +
				"    <part name=\"string\" type=\"s0:ArrayOfString\" />\r\n" +
				"  </message>\r\n" +
				"  <message name=\"UncompressListSoapOut\">\r\n" +
				"\r\n" +
				"    <part name=\"result\" type=\"s0:ArrayOfString\" />\r\n" +
				"  </message>\r\n" +
				"  <portType name=\"TextEJBSoap\">\r\n" +
				"    <operation name=\"Compress\">\r\n" +
				"      <input message=\"tns:CompressSoapIn\" />\r\n" +
				"      <output message=\"tns:CompressSoapOut\" />\r\n" +
				"    </operation>\r\n" +
				"    <operation name=\"Uncompress\">\r\n" +
				"      <input message=\"tns:UncompressSoapIn\" />\r\n" +
				"\r\n" +
				"      <output message=\"tns:UncompressSoapOut\" />\r\n" +
				"    </operation>\r\n" +
				"    <operation name=\"CompressList\">\r\n" +
				"      <input message=\"tns:CompressListSoapIn\" />\r\n" +
				"      <output message=\"tns:CompressListSoapOut\" />\r\n" +
				"    </operation>\r\n" +
				"    <operation name=\"UncompressList\">\r\n" +
				"      <input message=\"tns:UncompressListSoapIn\" />\r\n" +
				"      <output message=\"tns:UncompressListSoapOut\" />\r\n" +
				"\r\n" +
				"    </operation>\r\n" +
				"  </portType>\r\n" +
				"  <binding name=\"TextEJBSoap\" type=\"tns:TextEJBSoap\">\r\n" +
				"    <soap:binding transport=\"http://schemas.xmlsoap.org/soap/http\" style=\"document\" />\r\n" +
				"    <operation name=\"Compress\">\r\n" +
				"      <soap:operation soapAction=\"urn:Compress\" style=\"rpc\" />\r\n" +
				"      <input>\r\n" +
				"        <soap:body use=\"encoded\" namespace=\"java:net.soaptrace.webservices.TextEJB\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\r\n" +
				"      </input>\r\n" +
				"\r\n" +
				"      <output>\r\n" +
				"        <soap:body use=\"encoded\" namespace=\"java:net.soaptrace.webservices.TextEJB\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\r\n" +
				"      </output>\r\n" +
				"    </operation>\r\n" +
				"    <operation name=\"Uncompress\">\r\n" +
				"      <soap:operation soapAction=\"urn:Uncompress\" style=\"rpc\" />\r\n" +
				"      <input>\r\n" +
				"        <soap:body use=\"encoded\" namespace=\"java:net.soaptrace.webservices.TextEJB\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\r\n" +
				"      </input>\r\n" +
				"\r\n" +
				"      <output>\r\n" +
				"        <soap:body use=\"encoded\" namespace=\"java:net.soaptrace.webservices.TextEJB\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\r\n" +
				"      </output>\r\n" +
				"    </operation>\r\n" +
				"    <operation name=\"CompressList\">\r\n" +
				"      <soap:operation soapAction=\"urn:CompressList\" style=\"rpc\" />\r\n" +
				"      <input>\r\n" +
				"        <soap:body use=\"encoded\" namespace=\"java:net.soaptrace.webservices.TextEJB\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\r\n" +
				"      </input>\r\n" +
				"\r\n" +
				"      <output>\r\n" +
				"        <soap:body use=\"encoded\" namespace=\"java:net.soaptrace.webservices.TextEJB\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\r\n" +
				"      </output>\r\n" +
				"    </operation>\r\n" +
				"    <operation name=\"UncompressList\">\r\n" +
				"      <soap:operation soapAction=\"urn:UncompressList\" style=\"rpc\" />\r\n" +
				"      <input>\r\n" +
				"        <soap:body use=\"encoded\" namespace=\"java:net.soaptrace.webservices.TextEJB\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\r\n" +
				"      </input>\r\n" +
				"\r\n" +
				"      <output>\r\n" +
				"        <soap:body use=\"encoded\" namespace=\"java:net.soaptrace.webservices.TextEJB\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\r\n" +
				"      </output>\r\n" +
				"    </operation>\r\n" +
				"  </binding>\r\n" +
				"  <service name=\"TextEJB\">\r\n" +
				"    <port name=\"TextEJBSoap\" binding=\"tns:TextEJBSoap\">\r\n" +
				"      <soap:address location=\"http://127.0.0.1/TextEJB.asmx\" />\r\n" +
				"    </port>\r\n" +
				"\r\n" +
				"  </service>\r\n" +
				"</definitions>";
		}
	}
}
